﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using jnxc.DBUtility;//Please add references
namespace jnxc.DAL
{
	/// <summary>
	/// 数据访问类:farmRecord
	/// </summary>
	public partial class farmRecord
	{
		public farmRecord()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("farmId", "farmRecord"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int farmId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from farmRecord");
			strSql.Append(" where farmId=@farmId");
			SqlParameter[] parameters = {
					new SqlParameter("@farmId", SqlDbType.Int,4)
			};
			parameters[0].Value = farmId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(jnxc.Model.farmRecord model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into farmRecord(");
			strSql.Append("mhid,mpid,incomingArea,incomingWay,findTime,hostType,farmBH,inventoryCount,sampleCount,damageLevel,damageCount,representCount,investigationName,investigationTime,isProvineOwner,tcxlid,tcdid,aid,uid)");
			strSql.Append(" values (");
			strSql.Append("@mhid,@mpid,@incomingArea,@incomingWay,@findTime,@hostType,@farmBH,@inventoryCount,@sampleCount,@damageLevel,@damageCount,@representCount,@investigationName,@investigationTime,@isProvineOwner,@tcxlid,@tcdid,@aid,@uid)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@mhid", SqlDbType.Int,4),
					new SqlParameter("@mpid", SqlDbType.Int,4),
					new SqlParameter("@incomingArea", SqlDbType.NVarChar,50),
					new SqlParameter("@incomingWay", SqlDbType.NVarChar,50),
					new SqlParameter("@findTime", SqlDbType.DateTime),
					new SqlParameter("@hostType", SqlDbType.VarChar,20),
					new SqlParameter("@farmBH", SqlDbType.VarChar,20),
					new SqlParameter("@inventoryCount", SqlDbType.VarChar,10),
					new SqlParameter("@sampleCount", SqlDbType.VarChar,10),
					new SqlParameter("@damageLevel", SqlDbType.NVarChar,20),
					new SqlParameter("@damageCount", SqlDbType.VarChar,10),
					new SqlParameter("@representCount", SqlDbType.VarChar,10),
					new SqlParameter("@investigationName", SqlDbType.NVarChar,20),
					new SqlParameter("@investigationTime", SqlDbType.DateTime),
					new SqlParameter("@isProvineOwner", SqlDbType.Bit,1),
					new SqlParameter("@tcxlid", SqlDbType.Int,4),
					new SqlParameter("@tcdid", SqlDbType.Int,4),
					new SqlParameter("@aid", SqlDbType.Int,4),
					new SqlParameter("@uid", SqlDbType.Int,4)};
			parameters[0].Value = model.mhid;
			parameters[1].Value = model.mpid;
			parameters[2].Value = model.incomingArea;
			parameters[3].Value = model.incomingWay;
			parameters[4].Value = model.findTime;
			parameters[5].Value = model.hostType;
			parameters[6].Value = model.farmBH;
			parameters[7].Value = model.inventoryCount;
			parameters[8].Value = model.sampleCount;
			parameters[9].Value = model.damageLevel;
			parameters[10].Value = model.damageCount;
			parameters[11].Value = model.representCount;
			parameters[12].Value = model.investigationName;
			parameters[13].Value = model.investigationTime;
			parameters[14].Value = model.isProvineOwner;
			parameters[15].Value = model.tcxlid;
			parameters[16].Value = model.tcdid;
			parameters[17].Value = model.aid;
			parameters[18].Value = model.uid;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(jnxc.Model.farmRecord model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update farmRecord set ");
			strSql.Append("mhid=@mhid,");
			strSql.Append("mpid=@mpid,");
			strSql.Append("incomingArea=@incomingArea,");
			strSql.Append("incomingWay=@incomingWay,");
			strSql.Append("findTime=@findTime,");
			strSql.Append("hostType=@hostType,");
			strSql.Append("farmBH=@farmBH,");
			strSql.Append("inventoryCount=@inventoryCount,");
			strSql.Append("sampleCount=@sampleCount,");
			strSql.Append("damageLevel=@damageLevel,");
			strSql.Append("damageCount=@damageCount,");
			strSql.Append("representCount=@representCount,");
			strSql.Append("investigationName=@investigationName,");
			strSql.Append("investigationTime=@investigationTime,");
			strSql.Append("isProvineOwner=@isProvineOwner,");
			strSql.Append("tcxlid=@tcxlid,");
			strSql.Append("tcdid=@tcdid,");
			strSql.Append("aid=@aid,");
			strSql.Append("uid=@uid");
			strSql.Append(" where farmId=@farmId");
			SqlParameter[] parameters = {
					new SqlParameter("@mhid", SqlDbType.Int,4),
					new SqlParameter("@mpid", SqlDbType.Int,4),
					new SqlParameter("@incomingArea", SqlDbType.NVarChar,50),
					new SqlParameter("@incomingWay", SqlDbType.NVarChar,50),
					new SqlParameter("@findTime", SqlDbType.DateTime),
					new SqlParameter("@hostType", SqlDbType.VarChar,20),
					new SqlParameter("@farmBH", SqlDbType.VarChar,20),
					new SqlParameter("@inventoryCount", SqlDbType.VarChar,10),
					new SqlParameter("@sampleCount", SqlDbType.VarChar,10),
					new SqlParameter("@damageLevel", SqlDbType.NVarChar,20),
					new SqlParameter("@damageCount", SqlDbType.VarChar,10),
					new SqlParameter("@representCount", SqlDbType.VarChar,10),
					new SqlParameter("@investigationName", SqlDbType.NVarChar,20),
					new SqlParameter("@investigationTime", SqlDbType.DateTime),
					new SqlParameter("@isProvineOwner", SqlDbType.Bit,1),
					new SqlParameter("@tcxlid", SqlDbType.Int,4),
					new SqlParameter("@tcdid", SqlDbType.Int,4),
					new SqlParameter("@aid", SqlDbType.Int,4),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@farmId", SqlDbType.Int,4)};
			parameters[0].Value = model.mhid;
			parameters[1].Value = model.mpid;
			parameters[2].Value = model.incomingArea;
			parameters[3].Value = model.incomingWay;
			parameters[4].Value = model.findTime;
			parameters[5].Value = model.hostType;
			parameters[6].Value = model.farmBH;
			parameters[7].Value = model.inventoryCount;
			parameters[8].Value = model.sampleCount;
			parameters[9].Value = model.damageLevel;
			parameters[10].Value = model.damageCount;
			parameters[11].Value = model.representCount;
			parameters[12].Value = model.investigationName;
			parameters[13].Value = model.investigationTime;
			parameters[14].Value = model.isProvineOwner;
			parameters[15].Value = model.tcxlid;
			parameters[16].Value = model.tcdid;
			parameters[17].Value = model.aid;
			parameters[18].Value = model.uid;
			parameters[19].Value = model.farmId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int farmId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from farmRecord ");
			strSql.Append(" where farmId=@farmId");
			SqlParameter[] parameters = {
					new SqlParameter("@farmId", SqlDbType.Int,4)
			};
			parameters[0].Value = farmId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string farmIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from farmRecord ");
			strSql.Append(" where farmId in ("+farmIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnxc.Model.farmRecord GetModel(int farmId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 farmId,mhid,mpid,incomingArea,incomingWay,findTime,hostType,farmBH,inventoryCount,sampleCount,damageLevel,damageCount,representCount,investigationName,investigationTime,isProvineOwner,tcxlid,tcdid,aid,uid from farmRecord ");
			strSql.Append(" where farmId=@farmId");
			SqlParameter[] parameters = {
					new SqlParameter("@farmId", SqlDbType.Int,4)
			};
			parameters[0].Value = farmId;

			jnxc.Model.farmRecord model=new jnxc.Model.farmRecord();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnxc.Model.farmRecord DataRowToModel(DataRow row)
		{
			jnxc.Model.farmRecord model=new jnxc.Model.farmRecord();
			if (row != null)
			{
				if(row["farmId"]!=null && row["farmId"].ToString()!="")
				{
					model.farmId=int.Parse(row["farmId"].ToString());
				}
				if(row["mhid"]!=null && row["mhid"].ToString()!="")
				{
					model.mhid=int.Parse(row["mhid"].ToString());
				}
				if(row["mpid"]!=null && row["mpid"].ToString()!="")
				{
					model.mpid=int.Parse(row["mpid"].ToString());
				}
				if(row["incomingArea"]!=null)
				{
					model.incomingArea=row["incomingArea"].ToString();
				}
				if(row["incomingWay"]!=null)
				{
					model.incomingWay=row["incomingWay"].ToString();
				}
				if(row["findTime"]!=null && row["findTime"].ToString()!="")
				{
					model.findTime=DateTime.Parse(row["findTime"].ToString());
				}
				if(row["hostType"]!=null)
				{
					model.hostType=row["hostType"].ToString();
				}
				if(row["farmBH"]!=null)
				{
					model.farmBH=row["farmBH"].ToString();
				}
				if(row["inventoryCount"]!=null)
				{
					model.inventoryCount=row["inventoryCount"].ToString();
				}
				if(row["sampleCount"]!=null)
				{
					model.sampleCount=row["sampleCount"].ToString();
				}
				if(row["damageLevel"]!=null)
				{
					model.damageLevel=row["damageLevel"].ToString();
				}
				if(row["damageCount"]!=null)
				{
					model.damageCount=row["damageCount"].ToString();
				}
				if(row["representCount"]!=null)
				{
					model.representCount=row["representCount"].ToString();
				}
				if(row["investigationName"]!=null)
				{
					model.investigationName=row["investigationName"].ToString();
				}
				if(row["investigationTime"]!=null && row["investigationTime"].ToString()!="")
				{
					model.investigationTime=DateTime.Parse(row["investigationTime"].ToString());
				}
				if(row["isProvineOwner"]!=null && row["isProvineOwner"].ToString()!="")
				{
					if((row["isProvineOwner"].ToString()=="1")||(row["isProvineOwner"].ToString().ToLower()=="true"))
					{
						model.isProvineOwner=true;
					}
					else
					{
						model.isProvineOwner=false;
					}
				}
				if(row["tcxlid"]!=null && row["tcxlid"].ToString()!="")
				{
					model.tcxlid=int.Parse(row["tcxlid"].ToString());
				}
				if(row["tcdid"]!=null && row["tcdid"].ToString()!="")
				{
					model.tcdid=int.Parse(row["tcdid"].ToString());
				}
				if(row["aid"]!=null && row["aid"].ToString()!="")
				{
					model.aid=int.Parse(row["aid"].ToString());
				}
				if(row["uid"]!=null && row["uid"].ToString()!="")
				{
					model.uid=int.Parse(row["uid"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select farmId,mhid,mpid,incomingArea,incomingWay,findTime,hostType,farmBH,inventoryCount,sampleCount,damageLevel,damageCount,representCount,investigationName,investigationTime,isProvineOwner,tcxlid,tcdid,aid,uid ");
			strSql.Append(" FROM farmRecord ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" farmId,mhid,mpid,incomingArea,incomingWay,findTime,hostType,farmBH,inventoryCount,sampleCount,damageLevel,damageCount,representCount,investigationName,investigationTime,isProvineOwner,tcxlid,tcdid,aid,uid ");
			strSql.Append(" FROM farmRecord ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM VfarmRecord ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.farmId desc");
			}
			strSql.Append(")AS Row, T.*  from VfarmRecord T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "farmRecord";
			parameters[1].Value = "farmId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod
        public DataSet GetListByPagedis(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.tcbh desc");
            }
            strSql.Append(")AS Row, T.*  from (SELECT DISTINCT pname, pbh, cname, cbh, coname, cobh, tname, tbh, tcdname,tcdid FROM dbo.VfarmRecord ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(")  T ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
        public int GetRecordCountdis(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM (SELECT DISTINCT pname, pbh, cname, cbh, coname, cobh, tname, tbh, tcdname FROM dbo.VfarmRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(")  T");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        public DataSet GetListV(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM VfarmRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount7(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM (SELECT MTT.mhid,  coname,cname,MTT.pname,dbo.Vmyplant.pname AS plantname, dbo.Vmyharmful.hname, dbo.Vmyharmful.hbm, dbo.Vmyharmful.ldxm,[hostType],level1,level2,level3 FROM (	select mhid, [mpid],[hostType],coname,cname,pname,MAX( CASE damageLevel WHEN '重度' THEN sumarea ELSE 0 END) level3, max(CASE damageLevel WHEN '中度' THEN sumarea ELSE 0 END) level2,max(CASE  WHEN damageLevel in('轻度','轻度以下') THEN sumarea ELSE 0 END) level1 from ( SELECT mhid,[mpid],[hostType],coname,cname,pname,[damageLevel],sum([damageCount]) as sumarea FROM [farmRecord] inner join Vareapcct on [farmRecord].aid= Vareapcct.tid ");//);
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" group by [mhid],[mpid],[hostType],coname,cname,pname,[damageLevel]) as MT group by mhid, [mpid],[hostType],coname,cname,pname) as MTT INNER JOIN dbo.Vmyharmful ON MTT.mhid = dbo.Vmyharmful.mhid INNER JOIN dbo.Vmyplant ON MTT.mpid = dbo.Vmyplant.mpid) as TTT");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage7(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.mhid desc");
            }
            strSql.Append(")AS Row, T.*  from (SELECT MTT.mhid,  coname,cname,MTT.pname,dbo.Vmyplant.pname AS plantname, dbo.Vmyharmful.hname, dbo.Vmyharmful.hbm, dbo.Vmyharmful.ldxm,[hostType],level1,level2,level3 FROM (	select mhid, [mpid],[hostType],coname,cname,pname,MAX( CASE damageLevel WHEN '重度' THEN sumarea ELSE 0 END) level3, max(CASE damageLevel WHEN '中度' THEN sumarea ELSE 0 END) level2,max(CASE  WHEN damageLevel in('轻度','轻度以下') THEN sumarea ELSE 0 END) level1 from ( SELECT mhid,[mpid],[hostType],coname,cname,pname,[damageLevel],sum([damageCount]) as sumarea FROM [farmRecord] inner join Vareapcct on [farmRecord].aid= Vareapcct.tid ");//" T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append("group by [mhid],[mpid],[hostType],coname,cname,pname,[damageLevel]) as MT group by mhid, [mpid],[hostType],coname,cname,pname) as MTT INNER JOIN dbo.Vmyharmful ON MTT.mhid = dbo.Vmyharmful.mhid INNER JOIN dbo.Vmyplant ON MTT.mpid = dbo.Vmyplant.mpid) as T");

            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
		#endregion  ExtensionMethod
	}
}

